package com.example.provider;


import com.example.provider.server.RpcServer;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author kangshuai
 */
@SpringBootApplication
public class Provider02Application implements CommandLineRunner {


    @Autowired
    RpcServer rpcServer;

    public static void main(String[] args) {
        SpringApplication.run(Provider02Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(50000)
            .connectionTimeoutMs(30000)
            .retryPolicy(exponentialBackoffRetry)
            .namespace("base")
            .build();
        client.start();

        String path = "/lg-provider/provider2";
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL)
                            .forPath(path, "127.0.0.1:8899".getBytes());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                rpcServer.startServer("127.0.0.1", 8899);
            }
        }).start();
    }
}
